<?php

namespace App\Admin\Controllers;

use App\Models\School_calendar;
use Encore\Admin\Auth\Database\Administrator;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class School_calendarController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.school_calendar'));
            $content->description(trans('admin.list'));
            
            $content->body($this->grid()->render());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.school_calendar'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.school_calendar'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(School_calendar::class, function (Grid $grid) {
            $grid->id(trans('admin.ID'))->sortable();
            
            $grid->picture(trans('admin.uri'))->image('', 100, 100);
            //$grid->desc(trans('admin.school_calendar_desc'))->sortable();
            $grid->desc(trans('admin.school_calendar_desc'))->display(function($desc) {
                return str_limit($desc, 30, '...');
            });
            $grid->status(trans('admin.status'))->editable('select', [0 => '启用', 1 => '禁用'])->sortable(); 

            $grid->actions(function ($actions) {
                $actions->disableView();
            });

            //禁用导出按钮
            $grid->disableExport();
            
            //禁用查询过滤器
            $grid->disableFilter();

            //禁用分页条
            $grid->disablePagination();

            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(School_calendar::class, function (Form $form) {
            $form->display('id', 'ID');
            // 修改上传目录且使用随机生成文件名 (md5(uniqid()).extension)
            $form->image('picture',trans('admin.choose_image'))->uniqueName();
            $form->textarea('desc', trans('admin.school_calendar_desc'))->rows(10);
            $form->select('status', trans('admin.status'))->options(['0'=> trans('admin.enable'),'1'=> trans('admin.disable')])->rules('required');
        });
    }
}
